package com.neusoft.servelt;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: duan
 * @Date: Create in 15:36 2020/10/19
 */
@WebServlet("/login")
/*
    /login就是该Servlet的地址，一个地址只能找到一个Servlet
    一个Servlet可以注册多个地址，但是一个地址只能找到一个Servlet
 */
public class LoginServlet extends HttpServlet {
    /*
            练习，当传入username为admin且password为123时，页面显示登录成功，否则
            显示用户名或密码错误
            1、创建login.html页面
            2、创建表单
            3、创建LoginServlet
            4、重写doPost
            5、获取表单参数
            6、比较打印结果
         */
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");
//        String account = req.getParameter("account");
//        String password = req.getParameter("password");
//        System.out.println("account:::" + account);
//        System.out.println("password:::" + password);
//        resp.setContentType("text/html;charset=UTF-8");//resp是响应，响应的内容由浏览器来解析
//        PrintWriter writer = resp.getWriter();
//        if ("admin".equals(account) && "123".equals(password)) {
//            writer.println("登录成功");
//        } else {
//            writer.println("登录失败");
//        }
//    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        System.out.println(account + ", " + password);
        req.getRequestDispatcher("main.jsp").forward(req, resp);
    }
}
